package com.ln.store.web.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ln.store.domain.Product;
import com.ln.store.service.ProductService;
import com.ln.store.service.Impl.ProductServiceImpl;
import com.ln.store.utils.PageModel;
import com.ln.store.web.base.BaseServlet;

/**
 * Servlet implementation class ProductServlet
 */
public class ProductServlet extends BaseServlet {
	
	/**
	 * 根据商品id获取商品详情
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException
	 */
	public String findProductByPid(HttpServletRequest request,HttpServletResponse response) throws SQLException {
		
		//获取商品id
		String pid = request.getParameter("pid");
		//调用业务层获取商品详情
		ProductService service = new ProductServiceImpl();
		Product product =  service.findProductByPid(pid);
		//将实例存入request
		request.setAttribute("product", product);
		return "/jsp/product_info.jsp";
	}
	
	/**
	 * 商品列表
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException 
	 */
	public String findProductsByCidWithPage(HttpServletRequest request,HttpServletResponse response) throws SQLException {
		
		//获取cid,currentPage
		String cid = request.getParameter("cid");
		int currentPage = Integer.parseInt(request.getParameter("num"));
		//调用业务层功能：以分页形式查询当前类别下商品信息
		//返回PageModel对象（1.当前页商品新 2.分页 3.url）
		ProductService service = new ProductServiceImpl();
		PageModel pm= service.findProductsByCidWithPage(cid,currentPage);
		//将PageModel对象放入request
		request.setAttribute("page", pm);
		return "/jsp/product_list.jsp";
	}
}
